Critical 
#Persistent
#SingleInstance force
#InstallKeybdHook
#UseHook On

SetTitleMatchMode, 3
#IfWinActive, RIFT

;~ Set the tray icon and remove the default menu
;Menu, Tray, NoStandard
;Menu, Tray, Icon, nb4r.ico

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;~ >> pixel monitoring

; the toggle bit is used to stop spamming
lastToggle=0

; infinite loop to check for pixel based communication
;OutputDebug, Entering pixel parsing loop
Loop
{
	if WinActive("RIFT")
    {
		; get modifier and key pixels  
		CoordMode, Pixel, Client
		PixelGetColor, mColor,  1, 1, Alt
		PixelGetColor, kColor, 11, 1, Alt 
		PixelGetColor, tColor, 21, 1, Alt
		
		; convert modifier color into modifier keys
		mCtrl := SubStr(mColor, 7, 2) 
		mAlt := SubStr(mColor, 5, 2)
		mShift := SubStr(mColor, 3, 2)
		
		; convert key color into ascii character and toggle bit
		kCharHex := "0x"+SubStr(kColor, 7, 2)
		SetFormat Integer, D
		kCharDec := kCharHex+0
		kChar := Chr(kCharDec)
	  
		; get toggle
		toggle := SubStr(tColor, 7, 2) 
		
		; check toggle to ensure we don't spam
		if toggle != %lastToggle% 
		{
			; update toggle
			lastToggle:=toggle  
	
			; covert to AHK hotkey syntax
			hotkeyString:=""
				 
			if mCtrl = FF
			{   
			  hotkeyString.="^" 
			}
			if mAlt = FF
			{ 
			  hotkeyString.="!"
			}
			if mShift = FF
			{ 
			  hotkeyString.="+" 
			}
			
			; final check to see if toggle is one of 2 valid values, this check
			; stops the script from sending keys after logging-in or a reload-ui
			; before NB4R has finished loading.
			if ((toggle == "00") OR (toggle == "FF"))
			{			
				;OutputDebug, NB4R %hotkeyString%%kChar%
				Send, %hotkeyString%%kChar%
			}
		}
  } 

  Sleep, 10
}

;~ << End of pixel monitoring
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

